<?php
/*
 * Created on Feb 13, 2005
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

require_once ($DOCUMENT_ROOT . '/includes/functions.php');

do_header ('Forgot password', '');

if ($submit)
{
	if (db_connect ())
	{
		// check if they actually are a user
		$query = "SELECT * FROM users WHERE name='$email'";
		$result = mysql_query ($query)
			or report_error ($PHP_SELF, mysql_error (), "failed: name selection", '0');
		if (mysql_num_rows($result) > 0)
		{
			// update the record with a new password
			$pass = rand ();
			
			$pass = crypt ($pass, 'password');
			
			$pass2 = crypt ($pass, 'bobcat');
			
			$query = "UPDATE users SET pass='$pass2' WHERE name='$email'";
			$result = mysql_query ($query)
				or report_error ($PHP_SELF, mysql_error (), "failed: pass update", '0');;
			
			// send an email to the address with new password
			$s = "bobcat password";
	    	$m = "your login name: $email\nyour password: $pass\nlog in at http://bobcat.hungrychild.org\n
please be sure to change your password once you log in\n
the bobcat team\nadmin@bobcat.hungrychild.org\n";
		    	
			mail ($email, $s, $m, "From: accounts@bobcat.hungrychild.org\r\n");
				
			echo "<p>email has been sent</p>";
			do_url ('/', 'Home');
		}
		else
		{
			echo "<p>failure: that email is not registerd here</p>";
			echo "<p><a href='$PHP_SELF'>try again</a></p>";
		}
	}
	else
	{
		report_error ($PHP_SELF, mysql_error (), "db connection failure", '0');
	}
}
else
{
?>
	<form method="post" action="<?php echo $PHP_SELF; ?>">

		<p>A new password will be sent to your email address.</p>

		<p>Email address:</p>
		<p><input type="text" name="email" size="30"></p>

		<p><input type="submit" value="Send me my new password" name="submit"></p>
	</form>
<?php
}

do_footer ();
?>
